package com.yl.chainStore.services.impl

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.entity.pay.SysCompanyPayItem
import com.yl.chainStore.entity.pay.SysCompanyPayItemType
import com.yl.chainStore.entity.sick.CustomerSubscribeLog
import com.yl.chainStore.entity.system.CompanyDictionary
import com.yl.chainStore.mapper.pay.SysCompanyPayItemMapper
import com.yl.chainStore.mapper.pay.SysCompanyPayItemTypeMapper
import com.yl.chainStore.mapper.sick.CustomerSubscribeLogMapper
import com.yl.chainStore.mapper.system.CompanyDictionaryMapper
import com.yl.chainStore.pojo.vo.pay.SystemCompanyPayItemSelectVo
import com.yl.chainStore.pojo.vo.sick.CustomerSubscribeLogSelectVo
import com.yl.chainStore.pojo.vo.system.CompanyDictionaryVo
import com.yl.chainStore.services.IWebSystemService
import com.yl.chainStore.util.payItemTypeListToTree
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class WebSystemServiceImpl(
	private val sysCompanyPayItemTypeMapper: SysCompanyPayItemTypeMapper,
	private val SysCompanyPayItemMapper: SysCompanyPayItemMapper,
	private val companyDictionaryMapper: CompanyDictionaryMapper,
	private val customerSubscribeLogMapper: CustomerSubscribeLogMapper,
): IWebSystemService {

	companion object {
		private val log = LoggerFactory.getLogger(IWebSystemService::class.java)
	}


	override fun addItemType(itemType: SysCompanyPayItemType): ResponseBody {
		log.info("系统管理 ==> 添加收费项目类型 type:{}",itemType)
		if(itemType.superId != 0L){
			val superType = sysCompanyPayItemTypeMapper.selectById(itemType.superId)
				itemType.itemType = superType.itemType
		}
		sysCompanyPayItemTypeMapper.insert(itemType)
		return returnSuccess()
	}

	override fun getItemTypeTree(): List<SysCompanyPayItemType> {
		log.info("系统管理 ==> 获取所有收费项目树状图")

		val selectList =
			sysCompanyPayItemTypeMapper.selectList(Wrappers.query<SysCompanyPayItemType?>()
				.isNull("dept_id")
				.eq("enabled",1)
			)
		return payItemTypeListToTree(selectList,0L)
	}

	override fun addPayItem(item: SysCompanyPayItem): ResponseBody {
		log.info("系统管理 ==> 添加收费项目 item:{}",item)
		val itemType = sysCompanyPayItemTypeMapper.selectById(item.typeId)
		item.type = itemType.itemType
		SysCompanyPayItemMapper.insert(item)
		return returnSuccess()
	}

	override fun getPayItemPage(
		page: Page<SysCompanyPayItem>,
		vo: SystemCompanyPayItemSelectVo
	): Page<SysCompanyPayItem> {
		log.info("系统管理 ==> 获取收费项目分页 page:{}，vo:{}",page,vo)
		return SysCompanyPayItemMapper.selectItemPage(page,vo,vo.typeId)
	}

	override fun addCompanyDictionary(companyDictionary: CompanyDictionary): ResponseBody {
		log.info("系统管理 ==> 添加一个字典组 companyDictionary:{}",companyDictionary)
		companyDictionaryMapper.insert(companyDictionary)
		return returnSuccess()
	}

	override fun getCompanyDictionary(page: Page<CompanyDictionary>,vo: CompanyDictionaryVo): Page<CompanyDictionary> {
		log.info("系统管理 ==> 获取字典组 page:{}，vo：{}",page,vo)
		return companyDictionaryMapper.getDictionaryList(page,vo)
	}

	override fun getAllSubscribeLog(vo: CustomerSubscribeLogSelectVo): List<CustomerSubscribeLog> {
		return customerSubscribeLogMapper.getSubscribeLogList(vo)
	}
}
